package com.goldsprite.projecttemplategenerator.test;

import com.goldsprite.projecttemplategenerator.testold.*;

import com.badlogic.gdx.Gdx;
import java.io.File;
import java.util.Map;

public class ProjectGenerator {
	public static void generateProject(Map<String, Object> configData) {
		try {
			// 加载 YAML 配置
			String downloadUrl = YamlUtils.getNestedVal(configData, "download-url", String.class);
			String outputDirectory = YamlUtils.getNestedVal(configData, "output-directory", String.class);
			String oldProjectName = YamlUtils.getNestedVal(configData, "project.project-name.old", String.class);
			String newProjectName = YamlUtils.getNestedVal(configData, "project.project-name.new", String.class);
			String oldPackageName = YamlUtils.getNestedVal(configData, "project.package-name.old", String.class);
			String newPackageName = YamlUtils.getNestedVal(configData, "project.package-name.new", String.class);

			File outputDir = Gdx.files.local(outputDirectory+"template.zip").file();
			String realOutputDir = outputDir.getAbsolutePath();
			File destDir = Gdx.files.local(outputDirectory + "/extracted-" + newProjectName).file();
			String destDirectory = destDir.getAbsolutePath();
			
			// 下载模板 ZIP
			File zipFile = HttpFileDownloader.downloadFile(downloadUrl, realOutputDir);

			// 解压模板到指定目录
			File destDirectoryFile = new File(destDirectory);
			ZipFileExtractor.unzip(zipFile, destDirectory);

			// 替换项目名
			PackageNameReplacer.replacePackageNames(destDirectoryFile, oldProjectName, newProjectName);

			// 替换包名
			PackageNameReplacer.replacePackageNames(destDirectoryFile, oldPackageName, newPackageName);

			System.out.println("项目生成成功，目录: " + destDirectory);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
	}
}
